import axios from 'axios'
import { showToast } from 'vant';
import { Promise } from 'core-js' //解决IE浏览器不兼容新的api

const _axios = axios.create({
    // baseURL: 'http://prod-cn.your-api-server.com/api/',
    // baseURL: 'http://192.168.2.34:9823/api/',
    baseURL: 'https://api.ukeynetwork.com/api/',
    // baseURL: 'https://uk.api.aptoken.xyz/api/',
    timeout: 30000 //设置超时时间
})
_axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//添加请求拦截器
_axios.interceptors.request.use((config) => {
    //在每次请求前获取并携带token
	if ((config.url.indexOf('/login') === -1) && (config.url.indexOf('/nonce') === -1)) {
		config.headers['token'] = window.localStorage.getItem('token')
	}
	return config
},(error) => {
    return Promise.reject(error)
})

//添加响应拦截器
_axios.interceptors.response.use((response) => {
	window.navigator.onLine ? '' : showToast('未连接到互联网')
    let { code,message } = response.data
	if(code === 10000){
		showToast(message);
		window.localStorage.setItem('token','')
	}
    if(code !== 200){
        showToast({message:message || 'error',type:'warning'})
    }
    return response
},(error) => {
    return Promise.reject(error)
})

export default _axios
